var session = require('./session');
var config = require('./config')

function login() {
  wx.login({
    success: res => {
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      var appid = 'wxf83569c0a7d5000e'; //填写微信小程序appid 
      var secret = 'd4c4f6c4fb62f8c2d591b16f7572d295'; //填写微信小程序secret 

      //调用request请求api转换登录凭证 
      wx.request({
        url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&grant_type=authorization_code&js_code=' + res.code,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          //获取openid 
          // that.globalData.openid = res.data.openid;
          session.set('xyznzzopenId', res.data.openid)
        }
      })
    }
  })
}

module.exports = {
  login: login
}